package com.graduationdesign.zengyan.graduationdesignapp.ui.activities.orgs;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.graduationdesign.zengyan.graduationdesignapp.R;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.model.Organization;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.model.User;
import com.graduationdesign.zengyan.graduationdesignapp.ui.activities.PresenterInjection;
import com.graduationdesign.zengyan.graduationdesignapp.ui.activities.UiInjection;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.BaseUiView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.IntentKey;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.commonAdapter.CommonAdapter;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.commonAdapter.base.ViewHolder;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.errorhandle.BaseErrorView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.errorhandle.CommonErrorView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.contact.OrgContact;
import com.graduationdesign.zengyan.graduationdesignapp.ui.presenter.OrgsPresenter;
import com.graduationdesign.zengyan.graduationdesignapp.utils.Config;
import com.graduationdesign.zengyan.graduationdesignapp.utils.Tools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OrgAllPersonActivity extends AppCompatActivity implements OrgContact.ShowOrgPeoples, OrgContact.ShowOrgInfo {

    @BindView(R.id.org_name)
    TextView orgName;

    @BindView(R.id.avatar)
    SimpleDraweeView avatar;

    @BindView(R.id.desc)
    TextView desc;

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    @BindView(R.id.base_ui)
    BaseUiView mBaseUiView;




    private List<User> mUserList = new ArrayList<>();
    private CommonAdapter<User> userCommonAdapter;
    private BaseErrorView mErrorView;
    private OrgContact.Presenter presenter;
    private int mOrgId = -1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_org_all_person);
        ButterKnife.bind(this);
        initArgument();
        initPresenter();
        initToolbar();
        initRecylerView();

    }

    private void initArgument() {
        mOrgId = getIntent().getIntExtra(IntentKey.ORG_ID, -1);

    }

    private void initPresenter() {

        BaseErrorView mErrorView = new CommonErrorView(this);
        presenter = PresenterInjection.provideOrgPresenter(
                OrgsPresenter.newBuilder()
                    .setMShowOrgPeoples(this)
                    .setmBaseErrorView(mErrorView)
                    .setShowOrgInfo(this)
        );

        presenter.loadPeopleInOrg(mOrgId, mBaseUiView);
        presenter.loadOrg(mOrgId,mBaseUiView);
    }

    private void initRecylerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        userCommonAdapter = new CommonAdapter<User>(this, R.layout.item_people, mUserList) {
            @Override
            protected void convert(ViewHolder holder, User user, int position) {
                holder.setText(R.id.name, user.getName());
                holder.setText(R.id.description, user.getDescription());
                holder.setOnClickListener(R.id.ripple_layout, v -> {
                    User user1 = mUserList.get(position);
                    OrgAllPersonActivity.this.startActivity(UiInjection.provideProfile(OrgAllPersonActivity.this, user1.getStudentNum()));
                });
                holder.setVisible(R.id.date, false);
            }
        };
        recyclerView.setAdapter(userCommonAdapter);
    }

    private void initToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_menu);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(null);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        Tools.setSystemBarColor(this, R.color.grey_900);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_basic, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_search) {
            startActivity(UiInjection.provideSearchPeople(this));
        } else {
            Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void showOrgPeoples(List<User> users) {
        mUserList.clear();
        mUserList.addAll(users);
        userCommonAdapter.notifyDataSetChanged();
    }

    @Override
    public void showOrg(Organization organization) {
        orgName.setText(organization.getName());
        avatar.setImageURI(Config.imageUrl+organization.getAvatarUrl());
        desc.setText(organization.getDescription());
    }


}
